package com.ruoyi.project.infoprojectbasetype.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.infoprojectbasetype.domain.InfoProjectBaseType;
import com.ruoyi.project.infoprojectbasetype.service.IInfoProjectBaseTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 基础库项目类型Controller
 * 
 * @author ruoyi
 * @date 2022-11-28
 */
@RestController
@RequestMapping("/infoprojectbasetype/infoprojectbasetype")
public class InfoProjectBaseTypeController extends BaseController
{
    @Autowired
    private IInfoProjectBaseTypeService infoProjectBaseTypeService;

    /**
     * 查询基础库项目类型列表
     */
    @GetMapping("/list")
    public AjaxResult list(InfoProjectBaseType infoProjectBaseType)
    {
        List<InfoProjectBaseType> list = infoProjectBaseTypeService.selectInfoProjectBaseTypeList(infoProjectBaseType);
        List<TreeNode<Long>> nodeList = CollUtil.newArrayList();
        for (InfoProjectBaseType baseClassify : list) {
            nodeList.add(new TreeNode<>(baseClassify.getId(), baseClassify.getParentId(), baseClassify.getName(), 5));
        }
        List<Tree<Long>> treeList = TreeUtil.build(nodeList, 0l);
        return AjaxResult.success(treeList);
//        startPage();
//        List<InfoProjectBaseType> list = infoProjectBaseTypeService.selectInfoProjectBaseTypeList(infoProjectBaseType);
//        return getDataTable(list);
    }

    /**
     * 导出基础库项目类型列表
     */
    @PreAuthorize("@ss.hasPermi('infoprojectbasetype:infoprojectbasetype:export')")
    @Log(title = "基础库项目类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InfoProjectBaseType infoProjectBaseType)
    {
        List<InfoProjectBaseType> list = infoProjectBaseTypeService.selectInfoProjectBaseTypeList(infoProjectBaseType);
        ExcelUtil<InfoProjectBaseType> util = new ExcelUtil<InfoProjectBaseType>(InfoProjectBaseType.class);
        util.exportExcel(response, list, "基础库项目类型数据");
    }

    /**
     * 获取基础库项目类型详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(infoProjectBaseTypeService.selectInfoProjectBaseTypeById(id));
    }

    /**
     * 新增基础库项目类型
     */
    @Log(title = "基础库项目类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InfoProjectBaseType infoProjectBaseType)
    {
        return toAjax(infoProjectBaseTypeService.insertInfoProjectBaseType(infoProjectBaseType));
    }

    /**
     * 修改基础库项目类型
     */
    @Log(title = "基础库项目类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InfoProjectBaseType infoProjectBaseType)
    {
        int i = infoProjectBaseTypeService.updateInfoProjectBaseType(infoProjectBaseType);
        if (i == 500){
            return AjaxResult.error("当前类型不支持修改！");
        }
        return toAjax(i);
    }

    /**
     * 删除基础库项目类型
     */
    @PreAuthorize("@ss.hasPermi('infoprojectbasetype:infoprojectbasetype:remove')")
    @Log(title = "基础库项目类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(infoProjectBaseTypeService.deleteInfoProjectBaseTypeByIds(ids));
    }
}
